/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on 4-mrt-2012, 8:37:05
 */

package sokoban.view;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;

import sokoban.controller.Besturing;
import sokoban.model.Element;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
/**
 *
 * @author Admin
 */
public class MainWindow extends javax.swing.JFrame{
    Besturing besturingController;
    Highscore Highscore;

    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();

        // Maak een besturing controller aan
        besturingController = new Besturing(this);
    }

    public void displaySpeelveld(HashMap<String, Element> elementen){

        int xAantal = 20;
        int yAantal = 15;
        int grootte = 20;

        speelveld.removeAll();

        for (int i = 0; i < yAantal; i++){
            for (int j = 0; j < xAantal; j++){

                int xPositie = j * grootte;
                int yPositie = i * grootte;

                JLabel label = new JLabel();
                label.setBounds(xPositie, yPositie, grootte, grootte);
                label.setBackground(Color.GRAY);

                String lblNaam = i + "-" + j;
                    
                Element tmpElement = elementen.get(lblNaam);
                if(tmpElement != null){
                    String icon = tmpElement.getIcon();
                    Element.types type = tmpElement.getType();

                    String img = System.getProperty("user.dir") + "\\src\\sokoban\\image\\" + icon;
                    ImageIcon elementImage = new ImageIcon(img);

                    label.setIcon(elementImage);
                }

                label.setOpaque(true);
                speelveld.add(label);
            }
        }
        speelveld.setVisible(true);
        speelveld.repaint();

        speelveld.grabFocus();
    }

    private Boolean valideerToets(KeyEvent e){
        Boolean valideToets = false;

        switch (e.getKeyCode()){
            case KeyEvent.VK_UP:
                valideToets = true;
                break;
            case KeyEvent.VK_DOWN:
                valideToets = true;
                break;
            case KeyEvent.VK_RIGHT:
                valideToets = true;
                break;
            case KeyEvent.VK_LEFT:
                valideToets = true;
                break;
        }
       
        return valideToets;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        speelveld = new sokoban.view.Speelveld();
        jLabel1 = new javax.swing.JLabel();
        levelSelect = new javax.swing.JComboBox();
        gameSelect = new javax.swing.JComboBox();
        childMode = new javax.swing.JCheckBox();
        mainWindowSluitenBtn = new javax.swing.JButton();
        highscoreBtn = new javax.swing.JButton();
        startGame = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        stappenTellerTxt = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        speelveld.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                reageerOpToets(evt);
            }
        });

        javax.swing.GroupLayout speelveldLayout = new javax.swing.GroupLayout(speelveld);
        speelveld.setLayout(speelveldLayout);
        speelveldLayout.setHorizontalGroup(
            speelveldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );
        speelveldLayout.setVerticalGroup(
            speelveldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Sokoban");

        levelSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Level 1", "Level 2", "Level 3" }));

        gameSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Spel 1", "Spel 2", "Spel 3", "Spel 4" }));

        childMode.setText("Kindermodus");
        childMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                childModeActionPerformed(evt);
            }
        });

        mainWindowSluitenBtn.setText("Sluiten");
        mainWindowSluitenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainWindowSluitenBtnActionPerformed(evt);
            }
        });

        highscoreBtn.setText("Highscore");
        highscoreBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                highscoreBtnActionPerformed(evt);
            }
        });

        startGame.setText("Start");
        startGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startGameActionPerformed(evt);
            }
        });

        jLabel2.setText("Stappen");

        stappenTellerTxt.setEditable(false);
        stappenTellerTxt.setText("0");
        jScrollPane1.setViewportView(stappenTellerTxt);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mainWindowSluitenBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(highscoreBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE))
                    .addComponent(startGame, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(childMode, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(gameSelect, 0, 105, Short.MAX_VALUE)
                    .addComponent(levelSelect, 0, 105, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(speelveld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(speelveld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(levelSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(gameSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(childMode)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(startGame)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(highscoreBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(mainWindowSluitenBtn)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mainWindowSluitenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mainWindowSluitenBtnActionPerformed
        if(Highscore != null){Highscore.setVisible(false);}
        dispose();
        System.exit(0);
    }//GEN-LAST:event_mainWindowSluitenBtnActionPerformed

    private void highscoreBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_highscoreBtnActionPerformed
        Highscore = new Highscore();
        Highscore.setVisible(true);
    }//GEN-LAST:event_highscoreBtnActionPerformed

    private void childModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_childModeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_childModeActionPerformed

    private void startGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startGameActionPerformed
        boolean kindModus = childMode.isSelected();
        int niveau = levelSelect.getSelectedIndex() + 1;
        int spel = gameSelect.getSelectedIndex() + 1;
        int level = -1;

        if (niveau == 1){

            level = spel;
        }else if (niveau == 2){

            level = 4 + spel;
        }else{

            level = 8 + spel;
        }

        besturingController.startSpel(level, kindModus);
    }//GEN-LAST:event_startGameActionPerformed

    private void reageerOpToets(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_reageerOpToets
        if (valideerToets(evt)){
            switch (evt.getKeyCode()){
                case KeyEvent.VK_UP:
                    besturingController.doeZet("boven");
                    break;
                case KeyEvent.VK_DOWN:
                    besturingController.doeZet("onder");
                    break;
                case KeyEvent.VK_RIGHT:
                    besturingController.doeZet("rechts");
                    break;
                case KeyEvent.VK_LEFT:
                    besturingController.doeZet("links");
                    break;
            }
        }
    }//GEN-LAST:event_reageerOpToets

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox childMode;
    private javax.swing.JComboBox gameSelect;
    private javax.swing.JButton highscoreBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox levelSelect;
    private javax.swing.JButton mainWindowSluitenBtn;
    private sokoban.view.Speelveld speelveld;
    private javax.swing.JTextPane stappenTellerTxt;
    private javax.swing.JButton startGame;
    // End of variables declaration//GEN-END:variables

}
